<script setup lang="ts">
import { numberFormatter } from '@/plugins/number.plugin'
import { ref } from 'vue'
const { data } = defineProps(['data'])
const emit = defineEmits(['select', 'fyiCallback'])
const currentSelectedSku = ref()
const init = () => {
  data.list.map(e => {
    if (e.alias === data.default) {
      currentSelectedSku.value = e
    }
  })
}
init()

const handleSelectSku = (buffer: any) => {
  console.log(buffer)
  currentSelectedSku.value = buffer
  // emits
  emit('select', buffer)
}

const handleFyiCallback = () => {
  emit('fyiCallback')
}
</script>
<template>
  <div class="head-container">
    <div class="title">{{ data.name }}</div>
    <div class="related-txt">{{ data.sub }}</div>
    <div class="price">
      <span v-if="currentSelectedSku.fee > 0">+ {{ data.currency }} {{ numberFormatter(currentSelectedSku.fee, 100) }}</span>
      <span v-else>FREE</span>
    </div>
  </div>
  <div class="main-container">
    <div class="sku-container">
      <div
        :class="['sku-item', currentSelectedSku.alias === item.alias ? 'sku-item-active' : '']"
        v-for="item,index in data.list"
        :key="`sku-item-${index}-${item.alias}`"
        @click="handleSelectSku(item)"
        >
        <div class="sku-title">{{ item.title }}</div>
        <div class="sku-related-txt">{{ item.sub }}</div>
      </div>
    </div>
    <div class="related-info-container">
      <div
        class="related-info-item"
        v-for="item,index in data.relatedInfoList">
        <div class="image">
          <img :src="item.coverUrl" />
          <div class="play-btn" @click="handleFyiCallback">
            <ap-icon name="play" :size="20" color="rgba(0, 0, 0, .3)" />
          </div>
        </div>
        <div class="info">
          <div class="title">{{ item.title }}</div>
          <div class="sub">{{ item.sub }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.head-container {
  position: relative;
  .title {
    font-weight: bold;
  }
  .related-txt {
    color: #999999;
  }
  .price {}
}
.main-container {
  .sku-container {
    .sku-item {
      transition: all, 300ms;
      .sku-title {
        font-weight: bold;
      }
      .sku-related-txt {
        color: #999999;
      }
    }
    .sku-item-active {
      border-color: #4C9EEA;
    }
  }
  .related-info-container {
    .related-info-item {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: stretch;
      background: #FAFAFA;
      overflow: hidden;
      height: auto;
      .image {
        background: #333;
        flex: 0 1 auto;
        position: relative;
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
        .play-btn {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          background: rgba(255, 255, 255, .1);
          backdrop-filter: blur(5px);
          border-radius: 50%;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          cursor: pointer;
        }
      }
      .info {
        flex: 1;
        .title {
          font-weight: bold;
        }
        .sub {
          color: #999999;
        }
      }
    }
  }
}

// Mobile
@media screen and (max-width: 767px) {
  .head-container {
    .title {
      font-size: 20px;
    }
    .related-txt {
      font-size: 14px;
    }
    .price {
      font-size: 20px;
      position: absolute;
      right: 0;
      top: 0;
    }
  }
  .main-container {
    margin-top: 15px;
    .sku-container {
      width: 345;
      display: flex;
      flex-wrap: wrap;
      flex: 1;
      gap: 15px;
      .sku-item {
        width: 345px;
        border: 1px solid #EDEDED;
        padding: 15px;
        border-radius: 20px;
        cursor: pointer;
        .sku-title {
          font-size: 14px;
        }
        .sku-related-txt {
          margin-top: 5px;
          font-size: 14px;
        }
      }
      .sku-item-active {
        border-color: #4C9EEA;
      }
    }
    .related-info-container {
      width: 345px;
      margin-top: 15px;
      .related-info-item {
        border-radius: 20Px;
        .image {
          width: 100px;
          .play-btn {
            width: 30px;
            height: 30px;
          }
        }
        .info {
          width: calc(100% - 100px);
          padding: 15px 10Px;
          .title {
            font-size: 14px;
          }
          .sub {
            margin-top: 5px;
            font-size: 12px;
          }
        }
      }
    }
  }
}

// Tablet
@media screen and (max-width: 1023px) and (min-width: 768px) {
  .head-container {
    .title {
      font-size: 20px;
    }
    .related-txt {
      font-size: 14px;
    }
    .price {
      font-size: 20px;
      position: absolute;
      right: 0;
      top: 50%;
      transform: translateY(-50%);
    }
  }
  .main-container {
    margin-top: 15px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: flex-start;
    .sku-container {
      width: 465px;
      display: flex;
      flex-wrap: wrap;
      flex: 1;
      gap: 15px;
      .sku-item {
        width: 225px;
        border: 1px solid #EDEDED;
        padding: 15px;
        border-radius: 20px;
        cursor: pointer;
        .sku-title {
          font-size: 14px;
        }
        .sku-related-txt {
          margin-top: 5px;
          font-size: 14px;
        }
      }
      .sku-item-active {
        border-color: #4C9EEA;
      }
    }
    .related-info-container {
      width: 225px;
      .related-info-item {
        border-radius: 20Px;
        .image {
          width: 100px;
          img {}
          .play-btn {
            width: 30px;
            height: 30px;
          }
        }
        .info {
          width: calc(100% - 100px);
          padding: 15px 10Px;
          .title {
            font-size: 14px;
          }
          .sub {
            margin-top: 5px;
            font-size: 12px;
          }
        }
      }
    }
  }
}

// Desktop
@media screen and (min-width: 1024px) {
  .head-container {
    .title {
      font-size: 20Px;
    }
    .related-txt {
      font-size: 14Px;
    }
    .price {
      font-size: 20Px;
      position: absolute;
      right: 0;
      top: 50%;
      transform: translateY(-50%);
    }
  }
  .main-container {
    margin-top: 20Px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: flex-start;
    .sku-container {
      width: 620Px;
      display: flex;
      flex-wrap: wrap;
      flex: 1;
      gap: 20Px;
      .sku-item {
        width: 300Px;
        border: 1Px solid #EDEDED;
        padding: 15Px;
        border-radius: 20Px;
        cursor: pointer;
        &:hover {
          border-color: #999999;
        }
        .sku-title {
          font-size: 14Px;
        }
        .sku-related-txt {
          margin-top: 5Px;
          font-size: 14Px;
        }
      }
      .sku-item-active {
        border-color: #4C9EEA;
        &:hover {
          border-color: #4C9EEA;
        }
      }
    }
    .related-info-container {
      width: 300Px;
      .related-info-item {
        border-radius: 20Px;
        .image {
          width: 100Px;
          img {}
          .play-btn {
            width: 30Px;
            height: 30Px;
          }
        }
        .info {
          width: calc(100% - 100Px);
          padding: 15Px 10Px;
          .title {
            font-size: 14Px;
          }
          .sub {
            margin-top: 5Px;
            font-size: 12Px;
          }
        }
      }
    }
  }
}
</style>